package futbol.tacticas.concursantes2007.atiq;

import futbol.tacticas.Comando;
import futbol.tacticas.ComandoGolpearBola;
import futbol.tacticas.ComandoIrA;
import futbol.tacticas.ComandoPase;
import futbol.tacticas.ComandoTiroAPuerta;
import futbol.tacticas.SituacionJugadores;
import futbol.tacticas.concursantes.Tactica;

import java.awt.Color;

import java.awt.Point;

import java.util.Random;
import java.util.Vector;

public class TacticaKevinAtiq implements Tactica{
    
    private static final int IZQ = 1;
    private static final int DER = 2;
    private static final int SUP = 3;
    private static final int INF = 4;
    
    private static final double CAMPO_X = 160;
    private static final double CAMPO_Y = 120;
    private static final double PUERTA = 30;
    private static final double RANGO_CERCA_PUERTA_Y = PUERTA + 5;
    private static final int JUGADORES = 11;
    private static final double MEDIO = 40;
    private static final double SECTOR_1 = -120;
    private static final double SECTOR_2 = -80;
    private static final double SECTOR_3 = -50;
    private static final double SECTOR_4 = 0;
    private static final double SECTOR_5 = 50;
    private static final double SECTOR_6 = 80;
    private static final double SECTOR_7 = 120;
    private static final double ULTIMO = -140;
    
    private static final double VPLD = -110;
    private static final double PDD = 50;
    private static final double PDA = 110;
    
    private double posXAntBola = 0;
    private double posYAntBola = 0;
    private boolean col;
    
    private int datos[][]={
        {-160,0,5}, //arquero (1)
        
        {-100,40,6}, //defensa (2)
        {-100,-40,6}, //defensa (3)
        
        {-90,0,5}, //contencion (4)
        
        {-30,50,5}, // medio (5)
        {-30,-50,5}, // medio (6)
        
        {25,90,6}, // medio (7)
        {25,-90,6}, // medio (8)
        
        {120,60,6}, // delantero (9)
        {20,0,4}, // delantero (10)
        {120,-60,6} // delantero (11)
    };
   
    public TacticaKevinAtiq() {
        col = new Random().nextBoolean();
    } 
    
    public String getNombre() {
        return "Atiq F.C.";
    }

    public Color getColor1() {
        if (col)
            return new Color(51,153, 255);
        return Color.RED;
    }

    public Color getColor2() {
        if (col)
            return Color.WHITE;
         return Color.RED;
    }

    public Color getColorNumero() {
        if (col)
            return new Color(0, 51, 153);
        return Color.BLACK;
    }

    public int getFuerza(int n) {
        return datos[n][2];
    }

    public int getXInicial(int n) {
        return datos[n][0];
    }

    public int getYInicial(int n) {
        return datos[n][1];
    }

    public Vector<Comando> getComandos(SituacionJugadores sj) {
        Vector<Comando> comandos=new Vector<Comando>();
        Point bola=sj.getBola();
        double pbx = bola.getX();
        double pby = bola.getY();
        Point[] jugCon = sj.getContrario(); 
        int jmc = sj.getMiJugadorMasCerca(pbx, pby);
        accionMarcador (comandos, pbx, pby, jmc);
        accionContencion (comandos, pbx, pby, jmc);
        accionVolantePL (comandos, pbx, pby, jmc, jugCon);
        accionVolanteSL (comandos, pbx, pby, jmc);
        accionDelantero (comandos, pbx, pby, jmc);
        accion10 (comandos, pbx, pby, jmc);
        accionArquero(comandos, pbx, pby);
         posXAntBola = pbx;
         posYAntBola = pby;
        return comandos;
    }
    
    private void accionArquero (Vector<Comando> comandos, double bx, double by){
        if (by<=RANGO_CERCA_PUERTA_Y && by>=-RANGO_CERCA_PUERTA_Y && posHorizontal(bx)==IZQ)
            comandos.add(new ComandoIrA(0, -CAMPO_X, by));
        else
            comandos.add(new ComandoIrA(0, -CAMPO_X, 0));
        comandos.add(new ComandoGolpearBola (0, -CAMPO_X+10, by<0? -CAMPO_Y:CAMPO_Y));
    }
    
    private void accionMarcador(Vector<Comando> comandos, double bx, double by, int jmc){
        double rp = SECTOR_1;
        if (bx < SECTOR_1)
            rp = -CAMPO_X+10;
        if (jmc==1){
            comandos.add(new ComandoIrA (1, bx, by));
            comandos.add(new ComandoIrA (2, rp, PUERTA));
        }else if (jmc==2){
            comandos.add(new ComandoIrA (2, bx, by));
            comandos.add(new ComandoIrA (1, rp, -PUERTA));
        } else{
            double pneg = -PUERTA;
            double ras = -CAMPO_X-rp;
            double dis = -CAMPO_X-bx;
            double py2 = ((ras*(by-PUERTA))/dis)+PUERTA;
            double py3 = ((ras*(by+PUERTA))/dis)-PUERTA;
            comandos.add(new ComandoIrA (1, rp, py2>PUERTA ? PUERTA:py2));
            comandos.add(new ComandoIrA (2, rp, py3<pneg ? pneg:py3));
        }
        if (bx > SECTOR_2 || (by < MEDIO && by > -MEDIO)){
            int jug = (by > 0 ? 6 : 7);
            comandos.add(new ComandoPase (1, jug));
            comandos.add(new ComandoPase (2, jug));
        } else {
            comandos.add(new ComandoGolpearBola (1, 0, by + MEDIO));
            comandos.add(new ComandoGolpearBola (2, 0, by - MEDIO));
        }
    }
    
    private void accionContencion(Vector<Comando> comandos, double bx, double by, int jmc){
        if (bx > SECTOR_4){
            if (jmc == 3){
                comandos.add(new ComandoIrA (3, bx, by));
            } else if (bx < SECTOR_7){
                comandos.add(new ComandoIrA (3, SECTOR_3, by>0 ? MEDIO:-MEDIO));
            } else{
                comandos.add(new ComandoIrA (3, SECTOR_3, 0));
            }
            int jug = (by > 0 ? 8 : 10);
            comandos.add(new ComandoPase (3, jug));
        } else if (bx > SECTOR_2){
            comandos.add(new ComandoIrA (3, bx, by));
            int jug = (by > 0 ? 6 : 7);
            comandos.add(new ComandoPase (3, jug));
        } else {
            double rp = ULTIMO;
            if (bx < SECTOR_1)
                rp = -CAMPO_X+30;
            double ras = -CAMPO_X-rp;
            double dis = -CAMPO_X-bx;
            double py1 = ((ras*by)/dis);
            comandos.add(new ComandoIrA (3, rp, bx < SECTOR_1 ? (py1>PUERTA ? -15:15) : py1));
            if (by < MEDIO && by > -MEDIO){
                int jug = (by > 0 ? 6 : 7);
                comandos.add(new ComandoPase (3, jug));
            } else {
                int fac = (by > 0 ? 1 : -1);
                comandos.add(new ComandoGolpearBola (3, 0, by+(fac*MEDIO)));
            }
        }
    }
    
    private void accionVolantePL(Vector<Comando> comandos, double bx, double by, int jmc, Point[] jc){
        if (jmc == 4){
            comandos.add(new ComandoIrA (5, VPLD, -10));
            comandos.add(new ComandoIrA (4, bx, by));
        } else if (jmc == 5){
            comandos.add(new ComandoIrA (4, VPLD, 10));
            comandos.add(new ComandoIrA (5, bx, by));
        } else if (bx < SECTOR_3){
            if (posVertical(by)==SUP){
                comandos.add(new ComandoIrA (4, VPLD, 10));
                comandos.add(new ComandoIrA (5, bx, by));
            }
            else{
                comandos.add(new ComandoIrA (5, VPLD, -10));
                comandos.add(new ComandoIrA (4, bx, by));
            }
        } else{
            double jux = jc[0].getX();
            double jax = jc[1].getX();
            double juy = jc[0].getY();
            double jay = jc[1].getX();
            double jt = 0;
            if (jux > jax){
                jt = jax;
                jax = jux;
                jux = jt;
                jt = juy;
                juy = jay;
                jay = jt;
            }
            for (int i=2; i<JUGADORES; i++){
                jt = jc[i].getX();
                if (jux > jt){
                    jax = jux;
                    jux = jt;
                    jay = juy;
                    juy = jc[i].getY();
                } else if (jax > jt){
                    jax = jt;
                    jay = jc[i].getY();
                }
            }
            if (juy > jay){
                comandos.add(new ComandoIrA (4, jux+10, juy));
                comandos.add(new ComandoIrA (5, jax+10, jay));
            } else{
                comandos.add(new ComandoIrA (4, jax+10, jay));
                comandos.add(new ComandoIrA (5, jux+10, juy));
            }
        }
        if (bx > SECTOR_2 || (by < MEDIO && by > -MEDIO)){
            int jug = (by > 0 ? 6 : 7);
            comandos.add(new ComandoPase (4, jug));
            comandos.add(new ComandoPase (5, jug));
        } else {
            comandos.add(new ComandoGolpearBola (4, 0, by + MEDIO));
            comandos.add(new ComandoGolpearBola (5, 0, by - MEDIO));
        }
    }
    
    private void accionVolanteSL (Vector<Comando> comandos, double bx, double by, int jmc){
         if (jmc == 6 && bx < SECTOR_4){
             comandos.add(new ComandoIrA (6, bx, by));
             comandos.add(new ComandoIrA (7, SECTOR_5, -40));
         } else if (jmc == 7 && bx < SECTOR_4){
             comandos.add(new ComandoIrA (6, SECTOR_5, 40));
             comandos.add(new ComandoIrA (7, bx, by));
         } else if (bx < SECTOR_2){
            if (posVertical(by)==SUP){
                comandos.add(new ComandoIrA (6, SECTOR_3, 40));
                comandos.add(new ComandoIrA (7, SECTOR_3, -100));
            } else{
                comandos.add(new ComandoIrA (6, SECTOR_3, 100));
                comandos.add(new ComandoIrA (7, SECTOR_3, -40));
            }
        } else if (bx < SECTOR_3){
            if (posVertical(by)==SUP){
                comandos.add(new ComandoIrA (6, SECTOR_4, 40));
                comandos.add(new ComandoIrA (7, SECTOR_4, -80));
            } else{
                comandos.add(new ComandoIrA (6, SECTOR_4, 80));
                comandos.add(new ComandoIrA (7, SECTOR_4, -40));
            }
        }  else if (bx < SECTOR_4){
            if (posVertical(by)==SUP){
                comandos.add(new ComandoIrA (6, SECTOR_5, 40));
                comandos.add(new ComandoIrA (7, SECTOR_5, -100));
            } else{
                comandos.add(new ComandoIrA (6, SECTOR_5, 100));
                comandos.add(new ComandoIrA (7, SECTOR_5, -40));
            }
        } else if (bx < SECTOR_7){
            if (posVertical(by)==SUP){
                comandos.add(new ComandoIrA (6, SECTOR_5, 30));
                comandos.add(new ComandoIrA (7, bx, by));
            } else{
                comandos.add(new ComandoIrA (6, bx, by));
                comandos.add(new ComandoIrA (7, SECTOR_5, -30));
            }
        } else {
            comandos.add(new ComandoIrA (6, SECTOR_5, 60));
            comandos.add(new ComandoIrA (7, SECTOR_5, -60));
        }
        if (bx > SECTOR_6){
            comandos.add(new ComandoTiroAPuerta (6, 7));
            comandos.add(new ComandoTiroAPuerta (7, 7));
        } else{
            comandos.add(new ComandoPase (6, 8));
            comandos.add(new ComandoPase (7, 10));
        }
    }
    
    private void accionDelantero (Vector<Comando> comandos, double bx, double by, int jmc){
        if (jmc == 8){
            comandos.add(new ComandoIrA (8, bx, by));
            comandos.add(new ComandoIrA (10, PDA, -20));
        } else if (jmc == 10){
            comandos.add(new ComandoIrA (8, PDA, 20));
            comandos.add(new ComandoIrA (10, bx, by));
        } else if (bx < SECTOR_4){
            if (posVertical(by)==SUP){
                comandos.add(new ComandoIrA (8, PDA, 40));
                comandos.add(new ComandoIrA (10, PDD, -70));
            } else{
                comandos.add(new ComandoIrA (8, PDD, 70));
                comandos.add(new ComandoIrA (10, PDA, -40));
            }
        } else if (bx < SECTOR_5){
            comandos.add(new ComandoIrA (8, PDA, 20));
            comandos.add(new ComandoIrA (10, PDA, -20));
        } else {
            if (posVertical(by)==SUP){
                comandos.add(new ComandoIrA (8, PDA, 20));
                comandos.add(new ComandoIrA (10, bx, by));
            } else{
                comandos.add(new ComandoIrA (8, bx, by));
                comandos.add(new ComandoIrA (10, PDA, -20));
            }
        }
        if (bx > SECTOR_7+20 && (by<-100 || by>100)){
            comandos.add(new ComandoPase (8, 10));
            comandos.add(new ComandoPase (10, 8));
        } else {
            comandos.add(new ComandoTiroAPuerta (8, 5));
            comandos.add(new ComandoTiroAPuerta (10, 5));
        }
    }
    
    private void accion10 (Vector<Comando> comandos, double bx, double by, int jmc){
        int jug =  (posVertical(by)==SUP ? 8:10);
        if (jmc == 9 && bx < SECTOR_4){
            comandos.add(new ComandoIrA (9, bx, by));
            comandos.add(new ComandoPase (9, jug));
        } else if (bx < SECTOR_3){
            double posv = MEDIO;
            if (posVertical(by)==SUP)
                posv = -MEDIO;
            comandos.add(new ComandoIrA (9, SECTOR_4, posv));
            comandos.add(new ComandoPase (9, jug));
        }else if (bx < SECTOR_4){
            double posv = MEDIO+30;
            if (posVertical(by)==SUP)
                posv = -posv;
            comandos.add(new ComandoIrA (9, SECTOR_5, posv));
            comandos.add(new ComandoPase (9, jug));
        } else if (bx < SECTOR_7){
            comandos.add(new ComandoIrA (9, bx, by));
            comandos.add(new ComandoPase (9, jug));
        } else{
            comandos.add(new ComandoIrA (9, bx, by));
            if (bx > SECTOR_7+20 && (by<-100 || by>100)){
                comandos.add(new ComandoPase (9, jug));
            } else {
                comandos.add(new ComandoTiroAPuerta (9, 5));
            }
        }
    }
     
    private int posHorizontal (double bx){
        int dh = DER;
        if (bx<=0)
            dh = IZQ;
        return dh;
    }
    
    private int posVertical (double by){
        int dv = INF;
        if (by<=0)
            dv = SUP;
        return dv;
    }
}
